<template>
	<view>
		<navbar title="蓝奏云">
		</navbar>
		<view class="lanzou" v-if="!show">
			<view style="display: flex;flex-direction: column;align-items: center;">
				<image src="../../static/other/lanzou.png" mode="widthFix"></image>
				<h4 style="margin-top: 10px;">{{userInfo.lanzou.nickname}}</h4>
			</view>
			<u-alert @click="copy(userInfo.lanzou.domain)" title="个性域名" type="info"
				:description="userInfo.lanzou.domain" style="margin-top: 10px;"></u-alert>
			<u-button @click="open()" text="更新绑定" type="primary" style="margin-top: 10px;"></u-button>
			<u-button @click="openUrl('https://www.lanzoui.com/')" text="打开蓝奏云" style="margin-top: 10px;"></u-button>
		</view>
		<u-popup :show="show" :round="10">
			<view style="padding: 10px;">
				<h3>绑定蓝奏云</h3>
				<p style="margin: 10px 0;">
					绑定教程：打开蓝奏云之后登录即可绑定
				</p>
				<u-button text="打开蓝奏云" @click="open()" type="primary"></u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const wv = uni.requireNativePlugin('zz-mbrowser')
	import store from '@/store/index.js';
	export default {
		methods: {
			open() {
				try {
					let that = this
					wv.addEventListener('pageChange', e => {
						if (e.data.url.indexOf("/mydisk.php") >= 0) {
							const match = e.data.cookie.match(/(phpdisk_info=[^;]*)/);
							if (match) {
								that.getInfo(match[1])
								uni.showLoading({
									title: '数据获取中'
								});
								wv.close()
							} else {
								console.log("未找到 phpdisk_info 的值");
							}
						}
					})
					wv.open({
						initCookie: true,
						url: 'https://www.lanzoui.com/'
					})
				} catch (e) {
					console.log(e);
				}
			},
			getInfo(cookie) {
				let that = this
				uni.request({
					url: getApp().globalData.getLanZouInfo,
					data: {
						cookie: cookie
					},
					method: "POST",
					success(res) {
						that.userInfo.lanzou = res.data.data
						that.userInfo.lanzou.cookie = cookie
						store.commit('setUserInfo', that.userInfo)
						db.collection("uni-id-users").where({
							_id: that.userInfo._id
						}).update({
							lanzou: {
								cookie: cookie,
								nickname: res.data.data.nickname,
								domain: res.data.data.domain
							}
						})
						uni.hideLoading();
						that.show = false
					}
				})
			},
		},
		onLoad() {
			if (store.state.userInfo.lanzou.cookie == null) {
				this.show = true
			} else {
				this.show = false
			}
		},
		data() {
			return {
				show: true,
				userInfo: store.state.userInfo,
			};
		},

	}
</script>

<style>
	.lanzou {
		background-color: white;
		padding: 10px;
		margin: 10px;
		border-radius: 10px;
	}

	.lanzou image {
		width: 50%;
		border-radius: 10px;
	}
</style>